package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        Jedis jedis = jedisPool.getResource();
        //大集合减小集合
        Set<String> sdiff = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff!=null&&sdiff.size()>0){
            for (String fileName : sdiff) {
                //删除七牛云上的垃圾图片
                QiniuUtils.deleteFileFromQiniu(fileName);
                //删除redis里的垃圾图片
                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
                System.out.println("删除成功");
            }
        }
        jedis.close();
    }
}
